﻿using mds.Common;
using mds.Configuration;
using mds.ConfigurationService;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;

namespace mds.ConfigurationClient
{
    public class WebConfiguration
    {

        private static WebConfiguration _instance = new WebConfiguration();
        private string strSoulationId, strAppId, connectionString;
        public static WebConfiguration Instance
        {
            get { return _instance; }
        }
        private WebConfiguration()
        {
            strSoulationId = System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.SoulationIdName];
            connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[ConfigurationDefine.ConfigConnectionStringName].ConnectionString;            
        }
        public void InitConfig()
        {
            if (TestConfigType())
            {
                //探测是否需要更新配置文件
                if (TestVersion())
                {
                    //获取指定的版本
                    GetHistorySaveLocal();
                }
                else
                    //下载最新版本
                    SaveLocal();
            }
        }
        /// <summary>
        /// 检测配置模式
        /// </summary>
        /// <returns>true为远程配置文件false为本地固定配置</returns>
        private bool TestConfigType()
        {
            bool result = false;
            try
            {
                var isLocal = bool.Parse(System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.ConfigType]);
                if (isLocal)
                    result = true;
            }
            catch
            {
                result = false;
            }
            return result;
        }   
        private bool TestVersion()
        {
            Guid soulationId = Guid.Empty, appId = Guid.Empty;
            Guid.TryParse(strSoulationId, out soulationId);
            Guid.TryParse(strAppId, out appId);
            int hopeVersion = 0;
            try
            {
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.SoulationIdName], out hopeVersion);
            }
            catch
            {
                hopeVersion = 0;//没有配置，默认为0
            }
            if (hopeVersion > 0) return true;
            return false;
        }
        private void GetHistorySaveLocal()
        {
            Guid soulationId = Guid.Empty, appId = Guid.Empty;
            Guid.TryParse(strSoulationId, out soulationId);
            if (soulationId == Guid.Empty) return;
            Guid.TryParse(strAppId, out appId);
            if (appId == Guid.Empty) return;
            int hopeVersion = 0;
          
                int.TryParse(System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.SoulationIdName], out hopeVersion);
                var newSoluation = ConfigurationClient.Instance.GetConfig(soulationId, appId, hopeVersion);
            if (newSoluation == null) return;
            string localPath = System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.ConfigPath];
            if (Directory.Exists(localPath))
            {
                var list = Directory.GetFiles(localPath);
                if (list != null)
                {
                    for (var i = 0; i < list.Length; i++)
                    {
                        File.Delete(list[i]);
                    }
                }
            }
            XmlConfigSerializer.Instance.Serializer<SoluationInfo>(localPath + ConfigurationDefine.RootFile + ".xml", newSoluation.Soluation);
            foreach (var kv in newSoluation.DetailConfig)
            {
                File.AppendAllText(localPath + kv.AppplusId + ".xml", kv.Content);
            }
        }
        private void SaveLocal()
        {
            Guid soulationId = Guid.Empty, appId = Guid.Empty;
            Guid.TryParse(strSoulationId, out soulationId);
            if (soulationId == Guid.Empty) return;
            Guid.TryParse(strAppId, out appId);
            if (appId == Guid.Empty) return;
           var newSoluation=ConfigurationClient.Instance.GetNewConfig(soulationId, appId);
           if (newSoluation == null) return;
            string localPath = System.Configuration.ConfigurationManager.AppSettings[ConfigurationDefine.ConfigPath];
            if (Directory.Exists(localPath))
            {
                var list = Directory.GetFiles(localPath);
                if (list != null)
                {
                    for (var i = 0; i < list.Length; i++)
                    {
                        File.Delete(list[i]);
                    }
                }
            }
            XmlConfigSerializer.Instance.Serializer<SoluationInfo>(localPath + ConfigurationDefine.RootFile + ".xml", newSoluation.Soluation);
            foreach (var kv in newSoluation.DetailConfig)
            {
                File.AppendAllText(localPath + kv.AppplusId + ".xml", kv.Content);
            }
        }
    }
}
